package com.gxa.controller.investment;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.Result;
import com.gxa.dto.investmentDto.InvestmentPlanDto;
import com.gxa.entity.investment.InvestmentPlan;
import com.gxa.entity.investment.InvestmentPlanChild;
import com.gxa.service.investment.InvestmentPlanChildService;
import com.gxa.service.investment.InvestmentPlanService;
import com.gxa.vo.investmentVo.InvestmentPlanName;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "招商计划/招商计划")
@RestController
@RequestMapping("/investment")
public class InvestmentPlanController {

    @Autowired
    private InvestmentPlanService investmentPlanService;

    @Autowired
    private InvestmentPlanChildService investmentPlanChildService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("父列表(条件查询不能使用时")
    @GetMapping("/list")
    public Result seletAll(){

        return Result.success(investmentPlanService.selectAll());
    };

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("按条件查询")
    @PostMapping("/query")
    public Result query(@RequestBody InvestmentPlanDto investmentPlanDto, Integer pageNum, Integer pageSize){
        try {
            PageInfo result = investmentPlanService.queryByInvestmentPlanDto(investmentPlanDto, pageNum, pageSize);

            return Result.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("query failed");
        }

    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("子列表")
    @PostMapping("/query/child")
    public Result seletAllChild(@RequestBody InvestmentPlanName investmentPlanName){
        try {
            List<InvestmentPlanChild> result= investmentPlanChildService.queryByInvestmentPlanName(investmentPlanName.getInvestmentPlanName());
            return Result.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("xxxx failed");
        }


    };

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("添加父计划")
    @PostMapping("/add")
    public Result add(@RequestBody InvestmentPlan investmentPlan){
        try {
            investmentPlanService.add(investmentPlan);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("add 父 failed");
        }
        return Result.success("add 父 success");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("添加子计划")
    @PostMapping("/add/child")
    public Result addChild(@RequestBody InvestmentPlanChild investmentPlanChild){

        try {
            investmentPlanChildService.add(investmentPlanChild);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("add 子 failed");
        }
        return Result.success("add 子 success");

    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除父计划")
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable("id") Integer id){
        try {
            investmentPlanService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("del 子 failed");
        }
        return Result.success("del 子 success");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除子计划")
    @DeleteMapping("/del/child/{id}")
    public Result deleteChild(@PathVariable("id") Integer id){
        try {
            investmentPlanChildService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("del 子 failed");
        }
        return Result.success("del 子 success");

    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("父编辑接口")
    @PutMapping("/update")
    public Result update(@RequestBody InvestmentPlan investmentPlan){
        try {
            investmentPlanService.update(investmentPlan);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("update 父 failed");
        }
        return Result.success("update 父 success");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("子编辑接口")
    @PutMapping("/update/child")
    public Result updateChild(@RequestBody InvestmentPlanChild investmentPlanChild){

        try {
            investmentPlanChildService.update(investmentPlanChild);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("update 子 failed");
        }
        return Result.success("update 子 success");
    }
}
